<template>
  <div class="nav-header">
    <div class="fold" @click="iconClick">
      <el-icon v-if="isFold"><Expand /></el-icon>
      <el-icon v-else><Fold /></el-icon>
    </div>
    <div class="content">
      <div class="breadcrumb">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item
            v-for="item in breadcrumb"
            :key="item.name"
            :to="{ path: item.path || '#' }"
            >{{ item.name }}
          </el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div class="user-info">
        <el-dropdown :hide-on-click="false">
          <span class="el-dropdown-link">
            <el-avatar shape="square" :size="40" :src="avatar" />
            <el-link class="username" type="primary">{{ username }}</el-link>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="backLogin">退出登录</el-dropdown-item>
              <el-dropdown-item>用户信息</el-dropdown-item>
              <el-dropdown-item>系统管理</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, computed } from "vue";
import { ElMessageBox } from "element-plus";
import { useRouter, useRoute } from "vue-router";

import { useStore } from "@/store";
import avatar from "@/assets/img/avatar.jpg";
import localCache from "@/utils/localCache";
import { mapPathToBreadcrumb } from "@/utils/mapMenu";

export default defineComponent({
  emits: ["foldChange"],
  setup(props, { emit }) {
    const store = useStore();
    const router = useRouter();
    const route = useRoute();
    const isFold = ref(false);
    const username = computed(() => store.state.login.userInfo.name);
    const userMenus = computed(() => store.state.login.userMenu);
    const breadcrumb = computed(() =>
      mapPathToBreadcrumb(userMenus.value, route.path)
    );
    // 折叠菜单点击
    const iconClick = () => {
      isFold.value = !isFold.value;
      emit("foldChange", isFold.value);
    };
    // 退出登录
    const backLogin = () => {
      ElMessageBox.confirm("您确定要退出登录吗?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          router.push("/login");
          localCache.clearCache();
        })
        .catch(() => {
          console.log();
        });
    };
    return { isFold, iconClick, avatar, username, backLogin, breadcrumb };
  }
});
</script>

<style scoped lang="less">
.nav-header {
  width: 100%;
  display: flex;
  align-items: center;
  .fold {
    width: 20px;
    height: 20px;
    font-size: 20px;
    cursor: pointer;
  }
  .content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-left: 10px;
    flex: 1;
    .el-dropdown-link {
      display: flex;
      align-items: center;
      cursor: pointer;
      .username {
        margin-left: 10px;
      }
    }
  }
}
</style>
